package com.base.cn.platform.os.controller.qa;

import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.controller.cmmon.ApiBaseController;
import com.base.cn.platform.os.service.course.qa.qatype.QaTypeService;
import com.base.cn.platform.os.service.course.qa.question.QaQuestionService;
import com.base.cn.platform.os.service.course.qa.thumb.QaThumbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 问答数据接口
 *
 * @author s.li
 * @create 2018-09-06-16:00
 */
@RestController
@RequestMapping("/api/qa")
public class ApiQaQuestionController extends ApiBaseController {

    @Autowired
    private QaQuestionService qaQuestionService;
    @Autowired
    private QaThumbService qaThumbService;
    @Autowired
    private QaTypeService qaTypeService;

    /**
     * 初始化向老师提问页面接口
     * @param teacherId 老师ID
     * @return  Map<String,Object>
     */
    @RequestMapping("/initSaveQuestion")
    public Map<String,Object> initSaveQuestion(@RequestParam("teacherId") BigDecimal teacherId){
        Map<String,Object> qaType = qaTypeService.findQaTypeByTeacherId(teacherId);
        return ResultUtil.SUCCESS(qaType);
    }

    /**
     * 保存问答内容
     * @param userId 用户ID
     * @param imageStr 问答上传的图片，多个图片以“,”隔开
     * @param context 问答内容
     * @param teacherId 选择老师
     * @param typeId 问答类型ID
     * @param privacy 是否隐私提问：1.非隐私，2.隐私
     * @return Map<String,Object>
     */
    @RequestMapping("/saveQuestion")
    public Map<String,Object> saveQuestion(@RequestParam("userId") BigDecimal userId,
                                           @RequestParam(value = "imageStr",required = false) String imageStr,
                                           @RequestParam("context") String context,
                                           @RequestParam("teacherId") BigDecimal teacherId,
                                           @RequestParam("typeId") BigDecimal typeId,
                                           @RequestParam("privacy") int privacy,
                                           @RequestParam(value = "packageId",required = false,defaultValue = "0") BigDecimal packageId,
                                           @RequestParam(value = "courseId",required = false,defaultValue = "0") BigDecimal courseId,
                                           @RequestParam(value = "catalogId",required = false,defaultValue = "0") BigDecimal catalogId){

        Map<String,Object> params = new HashMap<>();
        int isFree =2;
        BigDecimal zero = new BigDecimal(0);
        if (packageId.compareTo(zero)!=0||courseId.compareTo(zero)!=0||catalogId.compareTo(zero)!=0) {
            isFree=1;
        }
        params.put("userId",userId);
        params.put("imageStr",imageStr);
        params.put("context",context);
        params.put("teacherId",teacherId);
        params.put("typeId",typeId);
        params.put("privacy",privacy);
        params.put("isFree",isFree);
        params.put("packageId",packageId);
        params.put("courseId",courseId);
        params.put("catalogId",catalogId);
        return qaQuestionService.saveQuestion(params);
    }

    /**
     * 加载老师下的问答数据列表
     * @param userId 用户ID
     * @param teacherId 老师ID
     * @param currentPage 页码
     * @return Map<String,Object>
     */
    @RequestMapping("/getQuestionListByTeacherId")
    public Map<String,Object> getQuestionListByTeacherId(@RequestParam(value = "userId",required = false,defaultValue = "0") BigDecimal userId,
                                                         @RequestParam("teacherId") BigDecimal teacherId,
                                                         @RequestParam(value = "currentPage",required = false) int currentPage){
        Map<String,Object> params = new HashMap<>();
        params.put("selectFrom",2);
        params.put("teacherId",teacherId);
        params.put("status",1);
        Map<String, Object> dataMap = qaQuestionService.queryQuestionListPage(currentPage, 10, params, userId,false, false, true, false, false, false, false,false);
        return ResultUtil.SUCCESS(dataMap);
    }

    /**
     * 获取问答详情
     * @param userId 用户ID
     * @param questionId 问答ID
     * @return Map<String,Object>
     */
    @RequestMapping("/findQuestionById")
    public Map<String,Object> findQuestionById(@RequestParam(value = "userId",required = false,defaultValue = "0") BigDecimal userId,
                                               @RequestParam("questionId") BigDecimal questionId){
        Map<String, Object> question = qaQuestionService.findQuestionByIdForFront(questionId, userId, false, true,
                true, true, false,
                false, false);
        return ResultUtil.SUCCESS(question);
    }

    /**
     * 问题点赞
     * @param userId 点赞用户ID
     * @param questionId 点赞的问答ID
     * @return Map<String,Object>
     */
    @RequestMapping("/thumbQuestion")
    public Map<String,Object> thumbQuestion(@RequestParam("userId") BigDecimal userId,
                                            @RequestParam("questionId") BigDecimal questionId){
        return qaThumbService.thumbQuestion(questionId,userId);
    }
}
